package com.app.micaihu.view.army.presenter

import com.app.base.mvp.BasePresenter
import com.app.http.NetRequestListener
import com.app.http.bean.BaseBean
import com.app.micaihu.configure.LoadType
import com.app.micaihu.manager.UserInforManager
import com.app.micaihu.utils.ApiServiceUtils.apiService
import com.app.micaihu.utils.ParamsMapUtils
import com.app.micaihu.view.army.contract.ArmyJoinedContract
import com.app.micaihu.view.bean.ArmySign
import com.app.micaihu.view.bean.QuestionList
import com.app.utils.biz.Biz

/**
 *已加入军团列表
 */
open class ArmyJoinedPresenter : BasePresenter<ArmyJoinedContract.View>(), ArmyJoinedContract.Presenter {

    private var isRefresh = true
    private var page = 1
    private var firstLoad = true
    private var lastGetTime = ""
    override fun armyList() {
        if (firstLoad) {
            getView()?.showLoading()
            firstLoad = false
        }
        val params = ParamsMapUtils.commonParams
        params["page"] = page
        params["type"] = 4
        params["mark"] = 0
        params["lastGetTime"] = lastGetTime
        if (Biz.instance.uid().isNotEmpty()) {
            params["uid"] = Biz.instance.uid()
        }
        requestNetwork(apiService.cmtList(params), object : NetRequestListener<BaseBean<QuestionList?>?> {
            override fun onSuccess(result: BaseBean<QuestionList?>?) {
                getView()?.hideLoading()
                result?.let { data ->
                    data.data?.let {
                        lastGetTime = it.lastGetTime
                    }
                }
                page++
                val loadType = if (isRefresh) LoadType.TYPE_REFRESH_SUCCESS else LoadType.TYPE_LOAD_MORE_SUCCESS
                getView()?.handleArmyList(result, loadType)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.hideLoading()
                val loadType = if (isRefresh) LoadType.TYPE_REFRESH_ERROR else LoadType.TYPE_LOAD_MORE_ERROR
                getView()?.handleArmyList(null, loadType)
                getView()?.onError(e)
            }
        })
    }

    override fun refresh() {
        page = 1
        isRefresh = true
        armyList()
    }

    override fun loadMore() {
        isRefresh = false
        armyList()
    }

    override fun armySignIn(gid: String) {
        val params = ParamsMapUtils.commonParams
        params["gid"] = gid
        params["uid"] = Biz.instance.uid()
        requestNetwork(apiService.armySignIn(params), object : NetRequestListener<BaseBean<ArmySign?>?> {
            override fun onSuccess(result: BaseBean<ArmySign?>?) {
                getView()?.hideLoading()
                getView()?.handleArmySignIn(result)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.hideLoading()
                getView()?.handleArmySignIn(null)
                getView()?.onError(e)
            }
        })
    }

}